//
//  AXRegisterViewController.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/10.
//  Copyright © 2018年 tzhg. All rights reserved.
//

#import "AXRegisterViewController.h"
#import "UIView+SDAutoLayout.h"
#import "ZXCategories.h"
#import "AXLoginViewController.h"
#import "HBDNavigationController.h"
#import "UIViewController+HBD.h"
#import "AXAccomplishRegisterVC.h"
#import "AXUrlListPartA.h"
#import "AXUrlLogin.h"

@interface AXRegisterViewController () <UITextFieldDelegate>
/** title */
@property (nonatomic ,strong) UILabel *titleLabel;
/**  手机图标*/
@property (nonatomic, strong) UIImageView *sjImageview;
/** 手机号码 */
@property (nonatomic ,strong) UITextField *sjTextField;
/**  获取验证码按钮*/
@property (nonatomic ,strong) UIButton *hqyzmButton;
/** 验证码图标 */
@property (nonatomic ,strong) UIImageView *yzmImageview;
/** 验证码TextField */
@property (nonatomic ,strong) UITextField *yzmTextField;
/** 请输入密码图标 */
@property (nonatomic ,strong) UIImageView *passwordImageview;
/** 请输入密码TextField */
@property (nonatomic ,strong) UITextField *passwordTextField;
/** 请再次输入密码图标 */
@property (nonatomic ,strong) UIImageView *password2Imageview;
/** 请再次输入密码TextField */
@property (nonatomic ,strong) UITextField *password2TextField;
/** 确定按钮 */
@property (nonatomic ,strong) UIButton *sureButton;

/** sessionId */
@property (nonatomic , strong) NSString *sessionId;

@property (nonatomic, strong)   NSTimer *timer;

@end

@implementation AXRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //客服按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(0, 0, 30, 30);
    [backBtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = rightItem;
    
    /** 搜索 */
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    self.titleLabel.text = @"注册";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:25];
    self.titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    self.navigationItem.titleView = self.titleLabel;
    
    [self creatView];
    [self setlayout];
    
   

}

- (void)creatView {
    
    /** 手机号码Imageview */
    self.sjImageview = [[UIImageView alloc]init];
    self.sjImageview.image = [UIImage imageNamed:@"sjhm"];
    [self.view addSubview:self.sjImageview];
    
    /** 手机号码TextField */
    self.sjTextField = [[UITextField alloc]init];
    self.sjTextField.textColor = [UIColor colorWithHex:0x999999];
    self.sjTextField.cornerRadius = 10;
    self.sjTextField.backgroundColor = [UIColor colorWithHex:0xf8f8f8];
    self.sjTextField.placeholder = @" 请输入手机号码";
    [self.view addSubview:self.sjTextField];
    
    /** 获取验证码 */
    self.hqyzmButton = [[UIButton alloc]init];
    [self.hqyzmButton addTarget:self action:@selector(hqyzm:) forControlEvents:UIControlEventTouchUpInside];
    [self.hqyzmButton setTitleColor:[UIColor colorWithHex:0xff9e05] forState:UIControlStateNormal];
    [self.hqyzmButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.hqyzmButton.titleLabel.font = FONT_SIXTEEN;
    [self.view addSubview:self.hqyzmButton];
    
    /** 验证码mageview */
    self.yzmImageview = [[UIImageView alloc]init];
    self.yzmImageview.image = [UIImage imageNamed:@"sjyz"];
    [self.view addSubview:self.yzmImageview];
    /** 验证码TextField */
    self.yzmTextField = [[UITextField alloc]init];
    self.yzmTextField.cornerRadius = 10;
    self.yzmTextField.placeholder = @" 请输入验证码";
    self.yzmTextField.backgroundColor = [UIColor colorWithHex:0xf8f8f8];
    self.yzmTextField.textColor = [UIColor colorWithHex:0x999999];
    [self.view addSubview:self.yzmTextField];
    
    /** 请输入密码Imageview  */
    self.passwordImageview = [[UIImageView alloc]init];
    self.passwordImageview.image = [UIImage imageNamed:@"mm"];
    [self.view addSubview:self.passwordImageview];
    /** 请输入密码TextField */
    self.passwordTextField = [[UITextField alloc]init];
    self.passwordTextField.cornerRadius = 10;
    self.passwordTextField.placeholder = @" 请输入密码";
    self.passwordTextField.backgroundColor = [UIColor colorWithHex:0xf8f8f8];
    self.passwordTextField.textColor = [UIColor colorWithHex:0x999999];
    [self.view addSubview:self.passwordTextField];
    
    /** 请再次输入密码Imageview */
    self.password2Imageview = [[UIImageView alloc]init];
    self.password2Imageview.image = [UIImage imageNamed:@"mm"];
    [self.view addSubview:self.password2Imageview];
    /** 请再次输入密码TextField */
    self.password2TextField = [[UITextField alloc]init];
    self.password2TextField.cornerRadius = 10;
    self.password2TextField.placeholder = @" 请再次输入密码";
    self.password2TextField.backgroundColor = [UIColor colorWithHex:0xf8f8f8];
    self.password2TextField.textColor = [UIColor colorWithHex:0x999999];
    [self.view addSubview:self.password2TextField];
  
    /** 确定按钮 */
    self.sureButton = [[UIButton alloc]init];
    [self.sureButton addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
    self.sureButton.backgroundColor = [UIColor colorWithHex:0x3bde72];
    [self.sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [self.sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.sureButton.cornerRadius = 10;
    [self.view addSubview:self.sureButton];
    
}

- (void)setlayout {
    
    
    
    /** 手机号码imageview*/
    self.sjImageview.sd_layout
    .leftSpaceToView(self.view, 50)
    .topSpaceToView(self.view, 155)
    .widthIs(30)
    .heightIs(30);
    /** 手机号码TextField*/
    self.sjTextField.sd_layout
    .leftSpaceToView(self.sjImageview, 10)
    .topSpaceToView(self.view, 150)
    .widthIs(SCREEN_WIDTH-180)
    .heightIs(40);
    /** 获取验证码 */
    self.hqyzmButton.sd_layout
    .rightSpaceToView(self.view, 50)
    .topSpaceToView(self.sjTextField, 0)
    .widthIs(100)
    .heightIs(40);
    /** 请输入验证码imageview*/
    self.yzmImageview.sd_layout
    .leftSpaceToView(self.view, 50)
    .topSpaceToView(self.hqyzmButton, 5)
    .widthRatioToView(self.sjImageview, 1)
    .heightRatioToView(self.sjImageview, 1);
    /** 请输入验证码TextField*/
    self.yzmTextField.sd_layout
    .leftSpaceToView(self.yzmImageview, 10)
    .topSpaceToView(self.hqyzmButton, 0)
    .widthRatioToView(self.sjTextField, 1)
    .heightRatioToView(self.sjTextField, 1);
    
    /** 请输入密码imageview*/
    self.passwordImageview.sd_layout
    .leftSpaceToView(self.view, 50)
    .topSpaceToView(self.yzmImageview, 50)
    .widthIs(30)
    .heightIs(30);
    /** 请输入密码TextField*/
    self.passwordTextField.sd_layout
    .leftSpaceToView(self.passwordImageview, 10)
    .topSpaceToView(self.yzmTextField, 40)
    .widthIs(SCREEN_WIDTH-180)
    .heightIs(40);

    /** 请再次输入密码imageview*/
    self.password2Imageview.sd_layout
    .leftSpaceToView(self.view, 50)
    .topSpaceToView(self.passwordImageview, 50)
    .widthIs(30)
    .heightIs(30);
    /** 请再次输入密码TextField*/
    self.password2TextField.sd_layout
    .leftSpaceToView(self.password2Imageview, 10)
    .topSpaceToView(self.passwordTextField,40)
    .widthIs(SCREEN_WIDTH-180)
    .heightIs(40);
    
    /** 登录*/
    self.sureButton.sd_layout
    .leftSpaceToView(self.view, 70)
    .topSpaceToView(self.password2TextField, 40)
    .widthIs(SCREEN_WIDTH -140)
    .heightIs(40);
    
    
}




#pragma mark - Network Request

//网络请求 注册时 获取验证码
- (void)hqyzm:(UIButton *)btn {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"fPhone"] = self.sjTextField.text;
    params[@"smsCodeType"] = @"REGISTUSER";
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_UserLogin, URL_UserLogin_notLoginSmsCode) success:^(id respose) {
        //判断验证码是否发送成功
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            [self.hqyzmButton setTitle:@"60s" forState:UIControlStateDisabled];
            self.hqyzmButton.backgroundColor = COLOR_ORG;
            [self.hqyzmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.hqyzmButton.cornerRadius = 5;
            self.hqyzmButton.borderWidth = 0;
            self.hqyzmButton.enabled = NO;
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
            /** 获取网络请求得到sessionId */
            self.sessionId = respose[@"header"][@"sessionId"];
            
        }
        
    } failure:^(NSError *error) {
        
         NSLog(@"登录失败%@",error);
    }];
    

}


/** 倒计时的方法 */

- (void)timerAction:(NSString *)timer
{
    NSString *title = self.hqyzmButton.titleLabel.text;
    int s = [title intValue];
    s--;
    if (s > 0) {
        title = [NSString stringWithFormat:@"%is", s];
        [self.hqyzmButton setTitle:title forState:UIControlStateDisabled];
    } else {
        self.hqyzmButton.backgroundColor = White(255);
        self.hqyzmButton.borderWidth = 1;
        self.hqyzmButton.enabled = YES;
        [self.timer invalidate];
    }
}



#pragma mark - Action

- (void)back:(UIButton *)btn {
    
    AXLoginViewController *logVC = [[AXLoginViewController alloc]init];
    logVC.hbd_barHidden = YES;
     [self.navigationController pushViewController:logVC animated:YES];
}

/** 确定按钮的事件 */

- (void)sure:(UIButton *)btn {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"sessionId"] = self.sessionId;
    params[@"fPhone"] = self.sjTextField.text;
    params[@"smsCode"] = self.yzmTextField.text;
    params[@"fPassword"] = self.passwordTextField.text;
    params[@"repeatPassword"] = self.password2TextField.text;
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_UserLogin, URL_UserLogin_RegisteredUserAccount) success:^(id respose) {
        
        /** 注册成功*/
        if ([respose[@"header"][@"statusCode"] integerValue] == 200){
            
            /** 如果注册成功就跳到注册完成界面 */
            AXAccomplishRegisterVC *resVC = [[AXAccomplishRegisterVC alloc]init];
            resVC.hbd_barAlpha = 0;
            [self.navigationController pushViewController:resVC animated:YES];
        }
        /**  */
        else {
            
            UIAlertController *alertController3 = [UIAlertController alertControllerWithTitle:@"提示" message:respose[@"header"][@"statusMessage"] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"点击了按钮1，进入按钮1的事件");
            }];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"点击了取消");
            }];
            
            [alertController3 addAction:action1];
            [alertController3 addAction:action2];
            [self presentViewController:alertController3 animated:YES completion:nil];
        }
        
    } failure:^(NSError *error) {
        
        
        NSLog(@"注册失败%@",error);
    }];
    
}



//点击空白处收起键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}



@end
